//
//  CAForumHeaderCell.m
//  cascade-realm
//
//  Created by QSC on 3/6/24.
//

#import "CAForumHeaderCell.h"
#import "CAImageBrowser.h"
#import "ImageCell.h"

@interface CAForumHeaderCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView * collectionView;
@property (nonatomic,strong)UIView *bottomView;
@property (nonatomic, strong) CAPageControl * PageControl;
@property (nonatomic,assign)CGFloat ImageH;
@property (nonatomic,strong)UIImageView * headImageV;
@property (nonatomic,strong)UIImageView * addImgv;
@property (nonatomic,strong)UILabel * nameLabel;
@property (nonatomic,strong)UILabel * titleLabel;
@property (nonatomic,strong)UILabel * contentLabel;
@end
@implementation CAForumHeaderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor greenColor];
        [self setUp];
    }
    return  self;
    
}

- (void)setModel:(CADataModel *)model{
    _model = model;
    self.PageControl.numberOfPages = model.allImages.count;
    
    [self.PageControl mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(model.allImages.count * 8 + 20);
    }];
    
    NSDictionary * dic = [DBTool getData:model.binduserId];
    CAHomeUser * user = [CAHomeUser mj_objectWithKeyValues:dic];
    [self.headImageV sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:Img(@"defult_head")];
    
    self.nameLabel.text = user.nickname;
    
    self.titleLabel.text = model.title;
    
    self.contentLabel.text = model.content;
    
    self.addImgv.hidden = [CAUserInfo.shared.followIds containsObject:model.binduserId];
    if (!self.addImgv.hidden) {
        self.addImgv.hidden = [model.binduserId isEqualToString:CAUserInfo.shared.userInfo.userId];
    }
   
}



- (void)setUp{
    
    UIView * contentView = self.contentView;
    contentView.clipsToBounds = YES;
    if (IsX) {
        self.ImageH = ScreenW / 375 * 460;
      
    }else{
        self.ImageH = ScreenW;
    }
    [contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(contentView);
        make.height.mas_equalTo(self.ImageH);
       
    }];
    
    CAPageControl * PageControl = [CAPageControl new];
   
    self.PageControl = PageControl;
   
    
    [self.contentView addSubview:PageControl];
   
    [PageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.collectionView).inset(25);
        make.centerX.equalTo(self.collectionView).offset(-12);
        make.width.mas_equalTo(self.model.allImages.count * 8 + 20);
        make.height.mas_equalTo(12);
    }];
 
    
    UIView * bottomView = [UIView new];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.layer.cornerRadius = 16;
    [contentView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentView);
        make.top.equalTo(self.collectionView.mas_bottom).offset(-10);
        
        make.bottom.equalTo(contentView).offset(10);
    }];
    
    UIImageView * iconImgV = [UIImageView new];
    iconImgV.layer.cornerRadius = 16;
    iconImgV.clipsToBounds = YES;
    [bottomView addSubview:iconImgV];
    [iconImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomView).offset(20);
        make.top.equalTo(bottomView).offset(12);
        make.size.mas_equalTo(32);
    }];
    self.headImageV = iconImgV;
    
    UIImageView * addImg = [[UIImageView alloc] initWithImage:Img(@"add_home")];
    [bottomView addSubview:addImg];
    [addImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(iconImgV);
        make.size.mas_equalTo(12);
    }];
    self.addImgv = addImg;
    
    UILabel * nameLabel = [UILabel LabelWithFont:Font(14) color:Hex(0x787878) text:@""];
    [bottomView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(iconImgV);
        make.left.equalTo(iconImgV.mas_right).offset(12);
        make.right.equalTo(bottomView).offset(-20);
    }];
    self.nameLabel = nameLabel;
    
    
    UILabel * titleLabel = [UILabel LabelWithFont:BoldFont(18) color:Hex(0x202020) text:@""];
    [bottomView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImgV);
        make.top.equalTo(iconImgV.mas_bottom).offset(20);
        make.right.equalTo(bottomView).offset(-20);
    }];
    titleLabel.numberOfLines = 0;
    self.titleLabel = titleLabel;
    
    UILabel * contentLabel = [UILabel LabelWithFont:Font(13) color:[UIColor colorWithWhite:0 alpha:0.5] text:@""];
    [bottomView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImgV);
        make.top.equalTo(titleLabel.mas_bottom).offset(16);
        make.right.equalTo(bottomView).offset(-20);
        make.bottom.equalTo(bottomView).offset(-20);
    }];
    contentLabel.numberOfLines = 0;
    self.contentLabel = contentLabel;
}


- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        
        layout.itemSize = CGSizeMake(ScreenW, self.ImageH);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
 
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
        
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        [collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
        // 去掉滚动条
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        _collectionView = collectionView;
    }
    return  _collectionView;;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.model.allImages.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    cell.imageV.image = Img(self.model.allImages[indexPath.row]);
  
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [CAImageBrowser ShowWithImages:self.model.allImages index:indexPath.item];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  
    self.PageControl.currentPage = scrollView.contentOffset.x / ScreenW;
 
    
}


@end
